package com.example.mapper;

import com.example.dto.*;
import com.example.pojo.Hospital;
import com.example.pojo.Orders;
import com.example.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrdersMapper {
    List<CalendarResponseDto> listOrdersAppointmentNumber(List<OrdersMapperDto> parameList);

    //插入订单,新增插入医生信息
    @Insert("INSERT INTO orders(orderDate, userId, hpId, smId,docId) "
            + "VALUES(#{orderDate}, #{userId}, #{hpId}, #{smId},#{docId})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "orderId")
    int insertOrder(Orders orders);

    // 查询是否存在重复记录
    @Select("SELECT COUNT(*) FROM orders WHERE orderDate = #{orderDate} " +
            "AND userId = #{userId} AND hpId = #{hpId} AND smId = #{smId} AND state = 1")
    int countDuplicateOrder(@Param("orderDate") String orderDate,
                            @Param("userId") String userId,
                            @Param("hpId") Integer hpId,
                            @Param("smId") Integer smId);

    //得到未支付的订单
    @Select("SELECT * FROM orders WHERE orderDate = #{orderDate} " +
            "AND userId = #{userId} AND hpId = #{hpId} AND smId = #{smId} AND state = 5")
    Orders getOrder(@Param("orderDate") String orderDate,
                    @Param("userId") String userId,
                    @Param("hpId") Integer hpId,
                    @Param("smId") Integer smId);

    // 根据用户id查询订单
    @Select("SELECT orders.orderId,orders.orderDate,orders.state,setmeal.name " +
            "FROM orders " +
            "JOIN setmeal ON orders.smId = setmeal.smId " +
            "WHERE userId = #{userId}")
    List<OrdersListDto> getOrdersByUserId(@Param("userId") String userId);

    //取消订单预约
    @Update("UPDATE orders SET state = 3 WHERE orderId = #{orderId}")
    void updateOrderStateToCancelled(Integer orderId);



    @Select("SELECT COUNT(*) FROM orders WHERE orderId = #{orderId}")
    int exists(Integer orderId);

    @Update("UPDATE orders SET state = 1 WHERE orderId = #{orderId}")
    void updateById(Orders orderId);


    @Select("""
            select o.orderId, o.orderDate, s.*
            from orders o
            left join setmeal s on s.smId = o.smId
            where o.orderId = #{orderId}
            """)
    OrdersDetailVDto selectDetailById(@Param("orderId") Integer orderId);

    @Select("SELECT * FROM orders WHERE orderId = #{orderId}")
    Orders getOrderById(Integer orderId);
}
